package com.cn.kabalifemerchants.ui.user.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.cn.kabalifemerchants.R;
import com.cn.kabalifemerchants.base.IBaseActivity;
import com.cn.kabalifemerchants.config.CustomRefreshListener;
import com.cn.kabalifemerchants.config.LoginHelper;
import com.cn.kabalifemerchants.config.RefreshHelper;
import com.cn.kabalifemerchants.ui.order.activity.ServiceOrderDetailsActivity;
import com.cn.kabalifemerchants.ui.user.adapter.NewMsgListAdapter;
import com.cn.kabalifemerchants.ui.user.bean.NewMsgListBean;
import com.cn.kabalifemerchants.utils.RxToast;
import com.cn.kabalifemerchants.view.NotDataView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by 晓林 on 2020/10/20.
 * function:
 */
public class NwsMsgActivity extends IBaseActivity<NwsMsgView, NwsMsgPresenter> implements NwsMsgView {

    @BindView(R.id.rv_data)
    RecyclerView rvData;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;


    List<NewMsgListBean.ResultBean.DataBean> mList;
    NewMsgListAdapter msgListAdapter;
    @BindView(R.id.tv_btn_order)
    TextView tvBtnOrder;
    @BindView(R.id.tv_btn_sys)
    TextView tvBtnSys;
    @BindView(R.id.tv_btn_nws)
    TextView tvBtnNws;
    private View notDataView;
    int type = 1;

    @Override
    protected NwsMsgPresenter createPresenter() {
        return new NwsMsgPresenter();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_new_msg;
    }

    @Override
    public void onReLoggedIn(String msg) {
        RxToast.error(msg);
        LoginHelper.againGoToLoginActivity(getMe());
    }

    @Override
    public void onError(String msg) {
        RxToast.error(msg);
    }

    @Override
    public Context _getContext() {
        return null;
    }

    @Override
    public void onMsgListSuccess(NewMsgListBean bean) {
        RefreshHelper.finishRefresh(refreshLayout, pagehttp);
        if (pagehttp == 1) {
            if (bean.getResult() != null && bean.getResult().getData().size() > 0) {
                msgListAdapter.replaceData(bean.getResult().getData());
            } else {
                msgListAdapter.replaceData(bean.getResult().getData());
                msgListAdapter.setEmptyView(notDataView);
            }
        } else {
            if (bean.getResult() != null && bean.getResult().getData().size() > 0) {
                msgListAdapter.addData(bean.getResult().getData());
            }

        }
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        setTitleBar("消息");
        mList = new ArrayList<>();
        LinearLayoutManager layoutManager3 = new LinearLayoutManager(getMe());
        layoutManager3.setOrientation(LinearLayoutManager.VERTICAL);
        layoutManager3.setSmoothScrollbarEnabled(true);
        layoutManager3.setAutoMeasureEnabled(true);
        rvData.setLayoutManager(layoutManager3);
        rvData.setNestedScrollingEnabled(false);
        //创建适配器
        msgListAdapter = new NewMsgListAdapter(R.layout.activity_new_msg_item, mList,type);
        //给RecyclerView设置适配器
        rvData.setAdapter(msgListAdapter);
        notDataView = NotDataView.notDataView(this, rvData, refreshLayout);
        msgListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //1：商品订单，2：服务订单，3：提现，4：详情，5：不跳
                if (msgListAdapter.getItem(position).getJump_type() == 1) {
//                    Bundle bundle = new Bundle();
//                    bundle.putInt("sign", 2);
//                    bundle.putInt("id", msgListAdapter.getItem(position).getOrder_id());
//                    $startActivity(MallOrderDetailsActivity.class, bundle);
                } else if (msgListAdapter.getItem(position).getJump_type() == 2) {
                    Bundle bundle = new Bundle();
                    bundle.putInt("id", msgListAdapter.getItem(position).getOrder_id());
                    $startActivity(ServiceOrderDetailsActivity.class, bundle);
                } else if (msgListAdapter.getItem(position).getJump_type() == 3) {
                    $startActivity(IMoneyTiXianListActivity.class);
                } else if (msgListAdapter.getItem(position).getJump_type() == 4) {
                    Bundle bundle = new Bundle();
                    bundle.putString("title", msgListAdapter.getItem(position).getTitle());
                    bundle.putString("content", msgListAdapter.getItem(position).getContent());
                    $startActivity(RichTextH5Activity.class, bundle);
                }
            }
        });
        initViewData(1);

        pagehttp = 1;
        getListData();
    }

    @Override
    public void initRefreshLayout() {
        RefreshHelper.initRefreshLayout(refreshLayout, new CustomRefreshListener() {
            @Override
            public void _onRefresh(RefreshLayout refreshLayout) {
                pagehttp = 1;
                getListData();
            }

            @Override
            public void _onLoadMore(RefreshLayout refreshLayout) {
                pagehttp++;
                if (null != mList) {
                    getListData();
                }
            }
        });
    }

    /**
     * 获取数据。刷新
     */
    public void getListData() {
        mPresenter.onMsgListData(LoginHelper.getLonginData().getResult(), pagehttp, pageNum, type);
    }


    @OnClick({R.id.tv_btn_order, R.id.tv_btn_sys, R.id.tv_btn_nws})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_btn_order:
                initViewData(1);
                refreshLayout.autoRefresh();
                break;
            case R.id.tv_btn_sys:
                initViewData(2);
                refreshLayout.autoRefresh();
                break;
            case R.id.tv_btn_nws:
                initViewData(3);
                refreshLayout.autoRefresh();
                break;
        }
    }

    private void initViewData(int index) {
        switch (index) {
            case 1:
                tvBtnOrder.setTextColor(getResources().getColor(R.color.font_000));
                tvBtnSys.setTextColor(getResources().getColor(R.color.text_909090));
                tvBtnNws.setTextColor(getResources().getColor(R.color.text_909090));
                type = 1;
                break;
            case 2:
                tvBtnOrder.setTextColor(getResources().getColor(R.color.text_909090));
                tvBtnSys.setTextColor(getResources().getColor(R.color.font_000));
                tvBtnNws.setTextColor(getResources().getColor(R.color.text_909090));
                type = 2;
                break;
            case 3:
                tvBtnOrder.setTextColor(getResources().getColor(R.color.text_909090));
                tvBtnSys.setTextColor(getResources().getColor(R.color.text_909090));
                tvBtnNws.setTextColor(getResources().getColor(R.color.font_000));
                type = 3;
                break;
        }
    }

}

